import java.util.Arrays;

public class SeqList {

    private int seqList[];
    private int usedSize;

    public SeqList(){
        this.seqList = new int[10];
    }


    public void display(){
        if(this.usedSize == 0){
            return;
        }

        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(seqList[i]);
        }
    }

    public void add(int data) throws FullException{
        if(isFull()){
            this.seqList =
                    Arrays.copyOf(this.seqList, 2*this.seqList.length);
            throw new FullException("数组溢出异常！");
        }

        this.seqList[this.usedSize] = data;
        this.usedSize++;
    }

    public void add(int pos, int data) throws FullException{
        if(isFull()){
            this.seqList =
                    Arrays.copyOf(this.seqList, 2*this.seqList.length);
            throw new FullException("数组溢出异常！");
        }

        if(pos < 0 || pos > usedSize){
            throw new PosException("pos不合法！");
        }

        for (int i = usedSize-1; i >= pos; i--) {
            this.seqList[i+1] = this.seqList[i];
        }

        this.seqList[pos] = data;

        this.usedSize++;
    }

    public boolean contains(int toFind) throws EmptyException{
        if(isEmpty()){
            throw new EmptyException("数组空异常！");
        }

        for (int i = 0; i < this.usedSize; i++) {
            if(this.seqList[i] == toFind){
                return true;
            }
        }

        return false;
    }

    public int indexOf(int toFind){
        if(isEmpty()){
            return -1;
        }

        for (int i = 0; i < this.usedSize; i++) {
            if(this.seqList[i] == toFind){
                return i;
            }
        }

        return -1;
    }

    public int get(int pos) throws PosException{
        if(isEmpty()){
            throw new EmptyException("数组为空！");
        }
        if(pos < 0 || pos >= this.usedSize){
            throw new PosException("pos不合法！");
        }

        return this.seqList[pos];
    }

    public void set(int pos, int value)throws PosException{
        if(isEmpty()){
            throw new EmptyException("数组为空！");
        }

        if(pos < 0 || pos >= this.usedSize){
            throw new PosException("pos不合法！");
        }

        this.seqList[pos] = value;
    }

    public void remove(int toRemove){
        if(isEmpty()){
            throw new EmptyException("数组为空！");
        }

//        for (int i = 0; i < this.usedSize; i++) {
//            if(toRemove == this.seqList[i]){
//                for (int j = i; j < this.usedSize-1; j++) {
//                    this.seqList[i] = this.seqList[i+1];
//                }
//                this.usedSize--;
//                break;
//            }
//        }
        //优化方法

        int index = indexOf(toRemove);

        if(index == -1){
            System.out.println("没有这个数字！");
            return;
        }

        for (int i = index; i < this.usedSize-1; i++) {
            this.seqList[i] = this.seqList[i+1];
        }

        this.usedSize--;
    }

    public int size(){
        return this.usedSize;
    }

    public void clear(){
        this.usedSize = 0;
    }
    public boolean isEmpty(){
        return this.usedSize == 0;
    }
    public boolean isFull(){
        return this.usedSize>=seqList.length;
    }
}
